package com.chaihu.intf.icommon;

import com.chaihu.bean.dto.PageDto;
import com.chaihu.bean.enums.MsgCode;
import com.chaihu.bean.result.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * 通用处理
 *
 * @author wangdongguang
 * @version 1.0
 * @date 2020/12/10
 **/
public abstract class BaseService<Response extends ResponseResult, In extends Serializable> /*extends SuperServiceImpl implements SuperService*/ {

    private static final Logger log = LoggerFactory.getLogger(BaseService.class);

    /**
     * 参数校验
     *
     * @param response
     * @param in
     * @return
     */
    public abstract Response checkHandle(Response response, In in);

    /**
     * 业务逻辑处理
     *
     * @param response
     * @param in
     * @return
     */
    public abstract Response doHandle(Response response, In in);

    /**
     * 响应封装
     *
     * @param response
     * @param in
     * @return
     */
    public abstract Response respHandle(Response response, In in);

    public Response mainHandle(Response response, In in) {

        log.trace("1.对服务进行业务逻辑校验");
        response = this.checkHandle(response, in);
        log.trace("2. 补充请求报文信息至应答报文");
        if (MsgCode.SUCCESS.getCode().equals(response.getCode())) {
            log.trace("3.对服务进行业务逻辑处理");
            response = this.doHandle(response, in);
            if (MsgCode.SUCCESS.getCode().equals(response.getCode())) {
                log.trace("4.设置服务业务逻辑返回信息");
                response = this.respHandle(response, in);
            }
        }
        return response;
    }

    //当前返回对象需要返回分页信息
    public void getPageList(Response response, In in, int selectTotal) {
        if (in instanceof PageDto) {
            PageDto pageDto = (PageDto) in;
            response.setPage(pageDto.getPage());
            response.setRows(pageDto.getRow());
            response.setRecords((int) Math.ceil((double) selectTotal / pageDto.getRow()));
            response.setTotal(selectTotal);
        }
    }
}